import json
import re
import urllib.request
import requests

csc = ''
with open('tsc.txt', 'r', encoding='utf-8') as f:
    csc = f.read()

with open('json.txt', 'r', encoding='utf-8') as f:
    content = f.read()
    content = content.replace("\\", "")
    #print(content)
    queryColumnsfind = '"queryColumns":(.*?),"summed"'
    query_columns = re.findall(queryColumnsfind, content, re.S)
    print(query_columns[0])

    query_result_find = '"queryResults":"(.*?)"}","contentType"'
    query_result = re.findall(query_result_find, content, re.S)
    print(query_result[0])

    summed_find = '"summed":"(.*?)","graphType"'
    summed = re.findall(summed_find, content, re.S)
    print(summed[0])

    columns_obj = json.loads(query_columns[0])
    #print(len(columns_obj))

    result_obj = json.loads(query_result[0])
    #print(len(result_obj))
    col_key = []
    rerere = '| '
    for i in columns_obj:
        uu=''
        try:
            uu = '('+i["unit"]+')'
        except KeyError:
            uu = ''

        rerere += (i['name']+uu + " | ")
        col_key.append(i["nameEn"])
    rerere += '\n'
    rerere += '|'
    for i in columns_obj:
        rerere += (":---:|")
    rerere += '\n'

    for j in result_obj:
        rerere += '| '
        for i in range(len(columns_obj)):
            coco = col_key[i]
            try:
                dada = j[coco]
            except KeyError:
                value = '键不存在'
                dada = '-'
            rerere += (str(dada) + " | ")
        rerere += '\n'

    #print(csc + rerere)

    print(csc+rerere)

    data = {
        "model": "gpt-4o",
        "messages": [
            {
                "role": "user",
                "content": csc + rerere
            }
        ]
    }
    data = json.dumps(data)

    r=requests.post(url='http://172.32.153.181:8901/v1/chat/completions', data=data,
                  headers={'Content-Type': 'application/json'})
    #print(r)
    dict_obj = json.loads(r.text)
    print("===========qwen 72b========")
    print(dict_obj["choices"][0]['message']['content'])


    data1 = {
        "model": "Qwen2.5-Math-7B-Instruct",
        "messages": [
            {
                "role": "user",
                "content": csc + rerere
            }
        ]
    }
    data1 = json.dumps(data1)
    #print(data1)
    r=requests.post(url='http://172.32.153.182:8909/v1/chat/completions', data=data1,
                  headers={'Content-Type': 'application/json'})
    #print(r)
    dict_obj = json.loads(r.text)
    print("===========Qwen2.5-Math-7B-Instruct========")
    print(dict_obj["choices"][0]['message']['content'])




    data2 = {
        "model": "deepseek-v3",
        "messages": [
            {
                "role": "user",
                "content": csc + rerere
            }
        ]
    }
    data2 = json.dumps(data2)

    r=requests.post(url='https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions', data=data2,
                  headers={'Content-Type': 'application/json','Authorization':'sk-77f3a5cb2a4f42c4be177967b62d3ed3','Host':'dashscope.aliyuncs.com'})
    #print(r)
    dict_obj = json.loads(r.text)
    #print(dict_obj)
    print("===========deepseek-v3========")
    print(dict_obj["choices"][0]['message']['content'])
    # with open('localsum.txt', 'w') as file:
    #     file.write(dict_obj["choices"][0]['message']['content'])
    #



'''
| 序号 | 姓名 | 地址 |
|:---:|:---:|:---:|
| 1  | 张三 | 合肥 |
| 2  | 李四 | 上海 |
| 3  | 王五 | 北京 |
'''
